package com.crack.nga2.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.crack.nga2.R;
import com.crack.nga2.custom.HeadLayoutImpl;
import com.crack.nga2.custom.ILoadingLayout;
import com.crack.nga2.entity.SubForum;
import com.crack.nga2.utils.GlobalConst;
import com.crack.nga2.volley.ModifySubForumRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 显示板块的子板块 通过ForumActivity启动
 */
public class SubForumActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
    /**
     * 显示某子版块
     */
    private static final int TYPE_DISPLAY = 1;
    /**
     * 隐藏某子版块
     */
    private static final int TYPE_HIDE = 2;
    private List<SubForum> forumList;
    private boolean isLoading = false;
    private int ufid;
    private ILoadingLayout header;
    private RequestQueue mQueue;

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub_forum);
        forumList = (List<SubForum>) getIntent().getSerializableExtra("list");
        mQueue = NgaApplication.getQueue();
        ufid = getIntent().getIntExtra("ufid", 0);
        TextView title = (TextView) findViewById(R.id.action_title);
        title.setText(getIntent().getStringExtra("forumName"));
        header = new HeadLayoutImpl(this, findViewById(R.id.pull_header));
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new SubForumAdapter());
        listView.setOnItemClickListener(this);
    }


    private Map<String, String> getParams(int type, int fid) {
        Map<String, String> params = new HashMap<>();
        params.put("func", "save_subscription");
        params.put("ufid", String.valueOf(ufid));
        params.put("fid", String.valueOf(fid));
        params.put("type", String.valueOf(type));
        params.put("raw", String.valueOf(3));
        return params;
    }

    private void post(final int type, final SubForum forum) {
        isLoading = true;
        header.startRefresh();
        String url = GlobalConst.BASE_URL + "nuke.php";
        Map<String, String> params = getParams(type, forum.getFid());
        mQueue.add(new ModifySubForumRequest(Request.Method.POST, url, params,
                new Response.Listener<Void>() {
                    @Override
                    public void onResponse(Void response) {
                        Toast.makeText(SubForumActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                        forum.isSelected = type == TYPE_DISPLAY;
                        setResult(1);
                        isLoading = false;
                        header.stopRefresh();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(SubForumActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                        isLoading = false;
                        header.stopRefresh();
                    }
                }));
    }


    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.action_return:
                finishForAnimation();
                break;
        }
    }

    @Override
    public void onClick(View v) {
        if (isLoading) {
            return;
        }
        switch (v.getId()) {
            case R.id.iv_icon_board:
                int position = (int) v.getTag();
                SubForum forum = forumList.get(position);
                ImageView iv = (ImageView) v;
                if (forum.isSelected) {
                    iv.setImageResource(R.drawable.subforum_normal_selector);
                    post(TYPE_HIDE, forum);
                } else {
                    iv.setImageResource(R.drawable.subforum_cheked_selector);
                    post(TYPE_DISPLAY, forum);
                }
                break;
            case R.id.iv_arrow:
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SubForum forum = (SubForum) parent.getItemAtPosition(position);
        Intent intent = new Intent(this, ForumActivity.class);
        intent.putExtra("forum", forum);
        startActivity(intent);
    }

    private class SubForumAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return forumList.size();
        }

        @Override
        public Object getItem(int position) {
            return forumList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return forumList.get(position).getFid();
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            ViewHolder holder;
            if (view == null) {
                view = View.inflate(SubForumActivity.this, R.layout.fragment_sub_forum_item, null);
                holder = new ViewHolder();
                holder.icon = (ImageView) view.findViewById(R.id.iv_icon_board);
                holder.arrow = (ImageView) view.findViewById(R.id.iv_arrow);
                holder.name = (TextView) view.findViewById(R.id.tv_name);
                holder.info = (TextView) view.findViewById(R.id.tv_surname);
                holder.icon.setOnClickListener(SubForumActivity.this);
                holder.arrow.setOnClickListener(SubForumActivity.this);
                holder.name.setTextColor(Color.BLACK);
                holder.info.setTextColor(getResources().getColor(R.color.small_text));
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            SubForum forum = forumList.get(position);
            holder.icon.setTag(position);
            holder.arrow.setTag(position);
            if (forum.isSelected) {
                holder.icon.setImageResource(R.drawable.subforum_cheked_selector);
            } else {
                holder.icon.setImageResource(R.drawable.subforum_normal_selector);
            }
            holder.name.setText(forum.getName());
            String info = forum.getInfo();
            if (info == null || info.length() == 0) {
                holder.info.setVisibility(View.GONE);
            } else {
                holder.info.setVisibility(View.VISIBLE);
                holder.info.setText(info);
            }
            return view;
        }
    }

    private class ViewHolder {
        ImageView icon;
        TextView name;
        TextView info;
        ImageView arrow;
    }
}
